package tralala.jdo;

import java.util.Date;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import tralala.data.Cancion;
import tralala.data.Reproduccion;
import tralala.data.Usuario;
import tralala.data.Cantante;
import tralala.jdo.*;
import tralala.server.TralalaManagerServer;
import tralala.server.TralalaService;
import tralala.server.remote.TralalaManager;


public class TralalaDAO implements ITralalaDAO{
	
	private static TralalaDAO tralalaDao = new TralalaDAO();
	private CancionDAO cancionDao;//= new CancionDAO();
	private CantanteDAO cantanteDao;//= new CantanteDAO();
	private ReproduccionDAO reproduccionDao;//= new ReproduccionDAO();
	private UsuarioDAO usuarioDao;//= new UsuarioDAO();
	private PersistenceManager pm= null;
	private PersistenceManagerFactory pmf=null;
	
	private TralalaDAO(){
		connect();
	}
	
	public static TralalaDAO getInstance(){
		return tralalaDao;
	}

	public synchronized PersistenceManager connect(){
		if(pm == null && pmf == null){
			try{
				System.out.println("Creando el PMF");
				pmf = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");
				System.out.println("Creando el PErsistence MAnager");
				pm = pmf.getPersistenceManager();		
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return pm;
	}
	public static void main(String[] args){
		
		String lanak = "kontuz lanak bazterbidean\n kontuz lanak bazterbidean\n kontuz lanak bazterbidean\n kontuz lanak bazterbidean\n"
				+ "kontuz lanak bazterbidean\n";
		String rickie = "LALALLALALALLALALA\n LALALLALALALLALALA\n LALALLALALALLALALA\n LALALLALALALLALALA\n LALALLALALALLALALA\n"
				+ "LALALLALALALLALALA\n";
		
		String cANCION = "HOPE HOE SADASD"
				+ "HOPE HOE SADASD";
		
		String atle = "HOPE HOE SADASDHOPE HOE SADASDHOPE HOE SADASDHOPE HOE SADASDHOPE HOE SADASD"
				+ "HOPE HOE SADASD"
				+ "HOPE HOE SADASD";
		
		String otra = "Living la vida loca\n Living la vida loca\n Living la vida loca\n Living la vida loca\n"
				+ "Living la vida loca\n Living la vida loca\n Living la vida loca\n Living la vida loca\n Living la vida loca\n";
		
		String steve = "We have turbulence\n We have turbulence\n We have turbulence\n We have turbulence\n We have turbulence\n We have turbulence\n"
				+ "We have turbulence\n We have turbulence\n We have turbulence\n We have turbulence\n We have turbulence\n We have turbulence\n v";
		
		Usuario u1 = new Usuario("kevincifu", "cifuentes","16258498J","kevinc@gmail.com","txikia");
		Usuario u2 = new Usuario("garcia", "cifuentes","1625822J","kevinc@gmail.com","txikia");
		Usuario u3 = new Usuario("ibañez", "cifuentes","16258494J","kevinc@gmail.com","txikia");
		Usuario u4 = new Usuario("amapolas", "cifuentes","16248498J","kevinc@gmail.com","txikia");
		Usuario u5 = new Usuario("diseEo", "cifuentes","16258434J","kevinc@gmail.com","txikia");
		
		
		Cantante c= new Cantante(1, 56, "pop", "Mikolapiz", "Marruecos");
		Cantante c1= new Cantante(2, 56, "pop", "MaxiBom", "Marruecos");
		Cantante c2= new Cantante(3, 56, "pop", "Negriton", "Marruecos");
		
		//Inicializar
		//TralalaDAO tdao= new TralalaDAO();
		
		// Constructor: Nombre, id_cantante, duracion, id_cancion, letra, precioRep, precioVit
		Cancion dg = new Cancion("Dead Gardens",1, "4:28",1, lanak,0.89, 3.00, c, "C:/canciones/musica.mpeg" );
		Cancion gls = new Cancion("Ghost Love Score",2, "10:46", 2, rickie, 0.89, 3.00, c1,"C:/canciones/musica.mpeg" );
		Cancion ltd = new Cancion("Letter to Dana",3,"6:00", 3, rickie, 0.89, 3.00, c2 , "C:/canciones/musica.mpeg");
		Cancion t = new Cancion("Tallulah", 4,"5:20", 4, atle,0.89, 3.00, c1, "C:/canciones/musica.mpeg");
		Cancion vs = new Cancion("Victorias Secret", 4,"4:43", 5, atle, 0.89,3.00, c2, "C:/canciones/musica.mpeg");
		Cancion n = new Cancion("Nightfall", 4,"5:34", 6, otra, 0.89, 3.00, c, "C:/canciones/musica.mpeg");
		Cancion a = new Cancion("Amaranth", 1,"3:51", 7, steve, 0.89, 3.00, c, "C:/canciones/musica.mpeg");
		CancionDAO s;
		s=CancionDAO.getInstance();
		
		s.almacenar(dg);
		s.almacenar(gls);
		s.almacenar(ltd);
		s.almacenar(t);
		s.almacenar(vs);
		s.almacenar(n);
		s.almacenar(a);
		
		UsuarioDAO m;
		m=UsuarioDAO.getInstance();
		m.almacenar(u1);
		m.almacenar(u2);
		m.almacenar(u3);
		m.almacenar(u4);
		m.almacenar(u5);
		
		System.out.println("[DAO] Hemos insertado todo con mucho cuidado y amor :).");
	}
	
	public Usuario login(String s, String p)
	{
		System.out.println("RECIBIDO: s, p:"+ s +" "+ p);
		return UsuarioDAO.getInstance().get(s, p);
	}

	public List<Cancion> buscarCanciones(String s) {
		System.out.println("Buscar Cancion: TralalaDAO");
		return CancionDAO.getInstance().buscarCancion(s);
	}

	public Cancion buscarCancion(String s) {
		System.out.println("Seleccionar Cancion: BuscarCancion: "+s);
		// TODO Auto-generated method stub
		Cancion c=CancionDAO.getInstance().obtenerCancion(s);
		ReproduccionDAO.getInstance().almacenar(new Reproduccion(new Date(System.currentTimeMillis()), TralalaService.usuarioCancion, c));
		return c;
	}

}
